package com.tbit.main.api;

import cn.hutool.core.collection.CollUtil;

import com.tbit.main.constant.UserConstant;
import com.tbit.main.exception.BaseException;
import com.tbit.main.pojo.*;
import com.tbit.main.service.*;
import com.tbit.maintain.api.StockMachineApi;
import com.tbit.maintain.dto.StockMachineDTO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@RestController
public class StockMachineApiImpl implements StockMachineApi {

    @Autowired
    private AccountUserService accountUserService;
    @Autowired
    private StockMachineService stockMachineService;
    @Autowired
    private AccountService accountService;
    @Autowired
    private RoleService roleService;
    @Autowired
    private RoleConfigService roleConfigService;
    @Autowired
    private MachineService machineService;

    /**
     * @description: 车辆入库
     * @author: mo.shanyong
     * @date: 2025/3/28 16:29
     * @param: stockMachineDTO
     */
    @Override
    public void add(StockMachineDTO stockMachineDTO) {
        //获取运维端完整登录数据
        AccountUser accountUser = this.getAccountUserById(stockMachineDTO.getAccountUserId(),stockMachineDTO.getUserCodeList().get(0));
        //车辆入库
        stockMachineService.add(stockMachineDTO.getUserCodeList().get(0),accountUser,stockMachineDTO.getWarehouseName(),
                String.valueOf(stockMachineDTO.getWarehouseId()),stockMachineDTO.getAccountId());
    }

    /**
     * @description: 车辆出库
     * @author: mo.shanyong
     * @date: 2025/3/28 16:33
     * @param: stockMachineDTO
     */
    @Override
    public void del(StockMachineDTO stockMachineDTO) {
        //获取运维端完整登录数据
        AccountUser accountUser = this.getAccountUserById(stockMachineDTO.getAccountUserId(),stockMachineDTO.getUserCodeList().get(0));
        //车辆出库
        stockMachineService.del(stockMachineDTO.getUserCodeList().get(0),accountUser,stockMachineDTO.getAccountId());
    }

    /**
     * @description: 获取运维端登录用户完整数据
     * @author: mo.shanyong
     * @date: 2025/3/31 13:51
     * @param: accountUserId
     * @return: com.tbit.main.pojo.AccountUser
     */
    private AccountUser getAccountUserById(Integer accountUserId,String userCode){
        AccountUser accountUser = accountUserService.getAccountUserById(accountUserId);
        /**账号类型转换*/
        //品牌、加盟商、区域代理商
        if (accountUser.getSubRoleType() == 0) {
            accountUser.setAccountUserType(accountUser.getAccountType());
            //运维管理员
        } else if (accountUser.getSubRoleType() == 1) {
            accountUser.setAccountUserType(UserConstant.ACCOUNT_MAINTAIN_MANAGER);
            //运维人员
        } else if (accountUser.getSubRoleType() == 2) {
            accountUser.setAccountUserType(UserConstant.ACCOUNT_MAINTAIN);
        } else {
            accountUser.setAccountUserType(accountUser.getSubRoleType());
        }
        /**判断登录权限*/
        Integer accountUserType = accountUser.getAccountUserType();
        Role role = roleService.getRoleId(accountUserType);
        /*if (!role.getLogin().equals(1) && !role.getLogin().equals(2)) {
            throw new BaseException("System.login.limit");
        }*/
        accountUser.setRole(role);
        List<RoleConfig> roleConfigs = roleConfigService.getRoleId(accountUserType);
        accountUser.setRoleConfig(roleConfigs);
        /** 获取运营区域信息 */
        List<Account> accounts = new ArrayList<>();
        // 品牌
        if (accountService.getCountHolderAgent(accountUser.getAccountUserId()) > 0) {
            accounts = accountService.getHolderAgent(accountUser.getAccountUserId());
        } else if (accountUserType.equals(UserConstant.ACCOUNT_ADMIN)) {//管理员可以对所有车辆进行操作
            Machine machine = machineService.getByUseCode(userCode);
            accounts.add(accountService.getByAccountId(machine.getAccountId()));
        } else if ((accountUserType.equals(UserConstant.ACCOUNT_BRAND) ||
                accountUser.getRole().getRoleLevel().equals(UserConstant.ACCOUNT_BRAND))
                && !accountUserType.equals(UserConstant.ACCOUNT_FRANCHISEE)
                && !accountUserType.equals(UserConstant.ACCOUNT_ADOPT)) {
            /** 加载品牌下的所有运营区域 */
            accounts = accountService.getBrandAgent(accountUser.getAccountId());
        }
        // 加盟商
        else if ((accountUserType.equals(UserConstant.ACCOUNT_AREA) ||
                accountUser.getRole().getRoleLevel().equals(UserConstant.ACCOUNT_AREA))
                && !accountUserType.equals(UserConstant.ACCOUNT_FRANCHISEE)) {
            /** 加载加盟商下面的所有运营区域 */
            accounts = accountService.getByParentId(accountUser.getAccountId());
        }
        //领养人
        else if (accountUserType.equals(UserConstant.ACCOUNT_ADOPT)) {
        }
        // 区域运营商、运维管理员、运维人员
        else {
            List<Integer> accountIds = new ArrayList<>();
            accountIds.add(accountUser.getAccountId());
            accounts = accountService.getByAccountIds(accountIds);
        }

        /**非领养人角色判断是有有运营区域*/
        if (!accountUserType.equals(UserConstant.ACCOUNT_ADOPT)) {
            if (CollUtil.isEmpty(accounts)) {
                throw new BaseException("System.noAgent");
            }
            List<Agent> agents = new ArrayList<>();
            for (Account a : accounts) {
                agents.add(new Agent(a.getAccountId(), a.getName(), a.getPhone()));
            }
            accountUser.setAgents(agents);

            /**设置区域运营商、运维管理员和运维人员归属区域id*/
            if (!accountUser.getAccountType().equals(UserConstant.ACCOUNT_BRAND) && !accountUser.getAccountType().equals(UserConstant.ACCOUNT_AREA)) {
                accountUser.setAgentId(accountUser.getAgents().get(0).getAccountId());
            }
        }

        return accountUser;
    }
}
